var app = {
    init: function() {
        this.ajax();
        this.closePop();
    },
    ajax: function() {
        var $dim = $('.ajaxLoading');

        $(document).on('ajaxStart', function(e, xhr, options) {
            $dim.show();
        }).on('ajaxStop', function(e, xhr, options) {
            $dim.hide();
        });
    },
    closePop: function(){
        var _self = this,
            $popGroup = $('.popGroup');

        $popGroup.find('.close').on('click',function(){
            $popGroup.hide();
        });

    },
    language: {
        ajaxError: '服务器没有响应，请稍后再试'
    }
}

app.init();


function ScrollLoad() {

}
/**

传递的参数：

1. tpl:模版script

2. data: 传递的参数

3. url: ajax 地址

4. type：方式

5. isPage0 是否第一页也是ajax

6. box: 要在哪个容器上做处理

7. stopLoad: 禁止加载，非必传



var configs = {

    tpl: $('.tpl'),

    data: {

    },

    url: '',

    type: 'POST',

    isPage0: false,

    box: $('.box')

}





 */
ScrollLoad.prototype.init = function(configs) {
    var _self = this,
        tpl = doT.template(configs.tpl.html());
    this.ajaxSettings = {
        success: function(data) {
            data = JSON.parse(data);
            configs.box.append(tpl(data));
        },
        error: function() {
            alert(app.language.ajaxError);
        },
        complete: function() {
            _self.loading = false;
        }
    };
    //是不是第一页也是ajax加载

    _self.page = configs.isPage0 ? 0 : 1;

    _self.loading = false;

    _self.stopLoad = configs.stopLoad  || function(){};

    $.extend(_self.ajaxSettings, {
        type: configs.type,
        url: configs.url,
        data: configs.data
    });

    if (configs.isPage0) {
        _self.loadAjax();
    }

    _self.listenScroll();
}

ScrollLoad.prototype.listenScroll = function() {
    var _self = this;
    $(window).on('scroll.loadMore', function() {
        var cH = $(window).height();
        var dH = $(document).height();
        var sT = document.body.scrollTop;
        if (sT + cH >= dH - 300) {
            _self.loadAjax();
        }
    });
}

ScrollLoad.prototype.loadAjax = function() {
    if (this.stopLoad()) return;
    if (this.loading) return;
    this.loading = true;

    var _self = this,
        _page = _self.page;

    $.extend(_self.ajaxSettings.data, {
        page: _page
    });

    $.ajax(_self.ajaxSettings);
}